package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ErrorResponseData;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGatewaySchedule;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizGatewayScheduleMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizGatewayScheduleParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayScheduleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * biz_gateway_schedule 服务实现类
 *
 * @author ssy
 * @date 2021/09/14 11:47
 */
@Slf4j
@Service
public class BizGatewayScheduleServiceImpl extends ServiceImpl<BizGatewayScheduleMapper, BizGatewaySchedule> implements BizGatewayScheduleService {

    @Resource
    private BizGatewayScheduleMapper bizGatewayScheduleMapper;


    /**
     * 查询所有biz_gateway_schedule
     *
     * @param bizGatewayScheduleParam
     * @return
     */
    @Override
    public List<BizGatewaySchedule> list(BizGatewayScheduleParam bizGatewayScheduleParam) {
        // 构造条件
        LambdaQueryWrapper<BizGatewaySchedule> queryWrapper = new LambdaQueryWrapper<>();
        if (bizGatewayScheduleParam != null && ObjectUtil.isNotEmpty(bizGatewayScheduleParam.getGatewayId())) {
            queryWrapper.eq(BizGatewaySchedule::getGatewayId, bizGatewayScheduleParam.getGatewayId());
        }
        return this.list(queryWrapper);
    }

    /**
     * 根据网关编号获取网关附表信息
     *
     * @param gatewayId
     * @return
     */
    @Override
    public BizGatewaySchedule oneByGatewayId(Integer gatewayId) {
        LambdaQueryWrapper<BizGatewaySchedule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizGatewaySchedule::getGatewayId, gatewayId);
        return bizGatewayScheduleMapper.selectOne(queryWrapper);
    }

    /**
     * 添加biz_gateway_schedule
     *
     * @param bizGatewayScheduleParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getCountByGateWay(BizGatewayScheduleParam bizGatewayScheduleParam) {
        Integer gateWayCount = baseMapper.getCountByGateWay(bizGatewayScheduleParam.getGatewayId());
        if (gateWayCount == 0) {
            this.add(bizGatewayScheduleParam);
        } else {
            new ErrorResponseData(500, "该网关已存在时间调度");
        }
    }

    /**
     * 分页查询biz_gateway_schedule
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<Map<String, Object>> pageList(BizGatewayScheduleParam param) {
        Page<Map<String, Object>> mapPage = PageFactory.defaultPage();
        if (param != null && ObjectUtil.isNotEmpty(param.getGatewayIds())) {
            String[] list = param.getGatewayIds().split(",");
            if (list.length > 0) {
                mapPage = this.baseMapper.pageList(PageFactory.defaultPage(), param, list);
            }
        }
        return new PageResult<>(mapPage);
    }

    /**
     * 添加biz_gateway_schedule
     *
     * @param bizGatewayScheduleParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BizGatewayScheduleParam bizGatewayScheduleParam) {
        // 将dto转为实体
        BizGatewaySchedule bizGatewaySchedule = new BizGatewaySchedule();
        BeanUtil.copyProperties(bizGatewayScheduleParam, bizGatewaySchedule);
        this.save(bizGatewaySchedule);
    }

    /**
     * 删除biz_gateway_schedule
     *
     * @param bizGatewayScheduleParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizGatewayScheduleParam bizGatewayScheduleParam) {
        this.removeById(bizGatewayScheduleParam.getId());
    }

    /**
     * 编辑biz_gateway_schedule
     *
     * @param bizGatewayScheduleParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(BizGatewayScheduleParam bizGatewayScheduleParam) {
        // 根据id查询实体
        BizGatewaySchedule bizGatewaySchedule = this.queryBizGatewaySchedule(bizGatewayScheduleParam);
        // 请求参数转化为实体
        BeanUtil.copyProperties(bizGatewayScheduleParam, bizGatewaySchedule);
        this.updateById(bizGatewaySchedule);
    }

    /**
     * 查看详情biz_gateway_schedule
     *
     * @param bizGatewayScheduleParam
     * @return
     */
    @Override
    public BizGatewaySchedule detail(BizGatewayScheduleParam bizGatewayScheduleParam) {
        return this.queryBizGatewaySchedule(bizGatewayScheduleParam);
    }


    /**
     * 获取biz_gateway_schedule
     *
     * @author ssy
     * @since 2021/09/14 11:47
     */
    private BizGatewaySchedule queryBizGatewaySchedule(BizGatewayScheduleParam bizGatewayScheduleParam) {
        return this.getById(bizGatewayScheduleParam.getId());
    }
}
